package com.pi.uc.facade;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.pi.uc.dao.entity.UcUserFollowEntity;
import com.pi.uc.enumerate.FollowStatus;
import com.pi.uc.vo.PrimeUserVo;
import com.pi.uc.vo.follow.UserFollowSketchVO;
import com.pi.uc.vo.follow.UserFollowVo;

@Service
public class UserFollowFacade {
  @Autowired
  private UserTransFacade userTransFacade;
  
  public List<UserFollowVo> transLateUserFollow(List<UcUserFollowEntity> userFollowList, List<Long> shareUsers){
    List<UserFollowVo> userFollowVos = new ArrayList<>();
    if(CollectionUtils.isEmpty(shareUsers)){
      return userFollowVos;
    }
    List<PrimeUserVo> primeList = 
        userTransFacade.transUsersToPrime(shareUsers);
    HashMap<Long, UcUserFollowEntity> userFollowMap = CollectionUtils.isEmpty(userFollowList)? new HashMap<>():userFollowList.stream().collect(
        Collectors.toMap(UcUserFollowEntity::getFollowUser, Function.identity(), 
            (key1, key2) -> key2, HashMap::new));
    for(PrimeUserVo primeUser:primeList){
      UserFollowVo follow = new UserFollowVo();
      if(userFollowMap.containsKey(primeUser.getUserId())
          && userFollowMap.get(primeUser.getUserId()).getFollowStatus()){
        follow.setFollow(FollowStatus.FOLLOW.getStatus());
      }
      follow.setUserId(primeUser.getUserId());
      follow.setName(primeUser.getName());
      follow.setAvatar(primeUser.getAvatar());
      userFollowVos.add(follow);
    }
    return userFollowVos;
  }

  public UserFollowSketchVO transLateUserFollowSketch(List<UcUserFollowEntity> userFollowList, List<Long> shareUsers) {
    UserFollowSketchVO sketch = new UserFollowSketchVO();
    List<UserFollowVo> userFollowVos = new ArrayList<>();
    sketch.setFellowList(userFollowVos);
    if(CollectionUtils.isEmpty(shareUsers)){
      sketch.setFollowCount(0);
      return sketch;
    }
   
    List<PrimeUserVo> primeList = 
        userTransFacade.transUsersToPrime(shareUsers);
    HashMap<Long, UcUserFollowEntity> userFollowMap = CollectionUtils.isEmpty(userFollowList)? new HashMap<>():userFollowList.stream().collect(
        Collectors.toMap(UcUserFollowEntity::getFollowUser, Function.identity(), 
            (key1, key2) -> key2, HashMap::new));
    int followCount = 0;
    for(PrimeUserVo primeUser:primeList){
      UserFollowVo follow = new UserFollowVo();
      if(userFollowMap.containsKey(Long.parseLong(primeUser.getUserId()))
          && userFollowMap.get(Long.parseLong(primeUser.getUserId())).getFollowStatus()){
        follow.setFollow(FollowStatus.FOLLOW.getStatus());
        followCount ++;
      }
      follow.setUserId(primeUser.getUserId());
      follow.setName(primeUser.getName());
      follow.setAvatar(primeUser.getAvatar());
      userFollowVos.add(follow);
    }
    sketch.setFollowCount(followCount);
    return sketch;
  }
}
